﻿Public Class ThongTinDoanhNghiep
    Private state As Boolean 'true là tạo mới
    Private Sub setReadonly(ByVal istrue As Boolean)
        Me.TenTextEdit.Properties.ReadOnly = istrue
        Me.EmailTextEdit.Properties.ReadOnly = istrue
        Me.DiaChiTextEdit.Properties.ReadOnly = istrue
        Me.DienthoaiTextEdit.Properties.ReadOnly = istrue
        Me.WebsiteTextEdit.Properties.ReadOnly = istrue
        Me.FaxTextEdit.Properties.ReadOnly = istrue
        Me.MaSoThueTextEdit.Properties.ReadOnly = istrue
    End Sub
    Private Sub BarButtonItemThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.Close()
    End Sub

    Private Sub BarButtonItemTaoMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        Try
            Me.setReadonly(False)
            Me.TenTextEdit.Focus()
            Me.ThongtindoanhnghiepBindingSource.AddNew()
            Me.state = True
            Me.BarButtonItemLuu.Enabled = True
            Me.BarButtonItemXoa.Enabled = True
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Frm_DonMau_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ThongtindoanhnghiepTableAdapter.Fill(Me.PcvgppDataSet.thongtindoanhnghiep)

        'Kiem tra xem co recode cua Doanh Nghiep chua, neu chua co thi moi cho tao moi neu co roi thi khong duoc tao moi
        If IsDBNull(Me.ThongtindoanhnghiepBindingSource) Then
            Me.BarButtonItemTaoMoi.Enabled = True
        Else : Me.BarButtonItemTaoMoi.Enabled = False
        End If

        setReadonly(True)
        Me.BarButtonItemLuu.Enabled = False
        Me.BarButtonItemXoa.Enabled = False
        'Me.GridView1.BestFitColumns()
    End Sub

    Private Sub BarButtonItemLuu_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Try
            Me.ThongtindoanhnghiepBindingSource.EndEdit()
            If Me.PcvgppDataSet.HasChanges Then
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Lưu' không ?", "NHAC NHO", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then

                    Me.ThongtindoanhnghiepTableAdapter.Update(Me.PcvgppDataSet.thongtindoanhnghiep)
                    Me.state = False
                    Me.setReadonly(True)
                    Me.BarButtonItemLuu.Enabled = False
                    Me.BarButtonItemXoa.Enabled = False
                    Me.BarButtonItemTaoMoi.Enabled = False
                End If
            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    'Private Function checkExisted()
    '    For index = 0 To Me.GridView1.RowCount - 1
    '        Try
    '            If Me.GridView1.GetRowCellValue(index, "ma") = Me.TenTextEdit.EditValue Then
    '                Return True
    '                Exit Function
    '            End If
    '        Catch ex As Exception

    '        End Try
    '    Next
    '    Return False
    'End Function

    'Private Sub MaTextEdit_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TenTextEdit.Validating
    '    If checkExisted() And state Then
    '        Me.TenTextEdit.ErrorText = "Mã đã tồn tại"
    '        e.Cancel = True

    '    End If
    'End Sub

    Private Sub BarButtonItemSua_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.BarButtonItemLuu.Enabled = True
        Me.BarButtonItemXoa.Enabled = True
        Me.setReadonly(False)
    End Sub

    Private Sub BarButtonItemFirst_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.ThongtindoanhnghiepBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonItem2_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        Me.ThongtindoanhnghiepBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonItem6_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem6.ItemClick
        Me.ThongtindoanhnghiepBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonItem7_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem7.ItemClick
        Me.ThongtindoanhnghiepBindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Xóa' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.ThongtindoanhnghiepBindingSource.RemoveCurrent()
            Me.ThongtindoanhnghiepTableAdapter.Update(Me.PcvgppDataSet.thongtindoanhnghiep)
            Me.state = False
            Me.setReadonly(True)
            Me.BarButtonItemLuu.Enabled = False
            Me.BarButtonItemXoa.Enabled = False
            Me.BarButtonItemTaoMoi.Enabled = False
        End If
    End Sub

    Private Sub EmailTextEdit_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MaSoThueTextEdit.Validating
        'If IsDBNull(Me.EmailTextEdit.EditValue) = True Then
        'Else
        '    If Me.EmailTextEdit.EditValue.ToString.Contains("@") = False Then
        '        Me.EmailTextEdit.ErrorText = "Email không hợp lệ"
        '        e.Cancel = True

        '    End If
        'End If

    End Sub

    Private Sub BarButtonItemHuyThayDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoi.ItemClick
        Me.ThongtindoanhnghiepBindingSource.EndEdit()
        If Me.PcvgppDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PcvgppDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

    End Sub

    'Private Sub FillByToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs, ByVal Me.NhomhangTableAdapter.Fill As GPP_PCV.pcvgppDataSetTableAdapters.NhomhangTableAdapter)
    '    Try
    '        Me.NhomhangTableAdapter.Fill(Me.PcvgppDataSet.nhomhang)
    '    Catch ex As System.Exception
    '        System.Windows.Forms.MessageBox.Show(ex.Message)
    '    End Try

    'End Sub

    Private Sub ThongtindoanhnghiepBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.ThongtindoanhnghiepBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.PcvgppDataSet)

    End Sub

End Class